package com.winit.openapi.wop.inbound.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.inbound.InboundOrderService;
import com.winit.oms.spi.inbound.command.InboundOrderImportDeclareRuleCommand;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
import com.winit.ups.spi.customstrade.ImportDeclarationRulesService;
import com.winit.ups.spi.customstrade.vo.ImportDeclarationRulesVo;

/**
 * 进口报关规则对外api
 * 
 * @version
 * 
 * <pre>
 * Author	Version		Date		Changes
 * qiyongkang 	1.0  		2017年7月25日 	Created
 * </pre>
 * 
 * @since 1.
 */
@Controller
@RequestMapping(value = "winit/ups/importDeclarationRule")
public class WHInboundImportDeclarationRuleController extends BaseController {

    @Resource
    private ImportDeclarationRulesService importDeclarationRulesService;

    @Resource
    private InboundOrderService           inboundOrderService;

    @RequestMapping(value = "/queryList")
    public String queryImportDeclarationRulesByCondition(@ModelAttribute RequestMsg requestMsg,
                                                         @ModelAttribute ResponseMsg responseMsg) {
        // 获取参数
        JSONObject json = null;
        if (requestMsg.getData() instanceof JSONObject) {
            json = JSONObject.parseObject(requestMsg.getData().toString());
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }

        // 校验
        ValidateUtil.isNotNull(json, "endWarehouseCode");
        ValidateUtil.validMaxLength(json, "endWarehouseCode", 40, true);
        ValidateUtil.isNotNull(json, "productCode");
        ValidateUtil.validMaxLength(json, "productCode", 32, true);

        // 获取【目的仓】编码
        String endWarehouseCode = (String) json.get("endWarehouseCode");
        // 获取产品编码
        String productCode = (String) json.get("productCode");
        // 获取订单号
        String winitOrderNo = (String) json.get("winitOrderNo");

        InboundOrderImportDeclareRuleCommand command = new InboundOrderImportDeclareRuleCommand();
        command.setEndWarehouseCode(endWarehouseCode);
        command.setProductCode(productCode);
        command.setWinitOrderNo(winitOrderNo);
        List<ImportDeclarationRulesVo> list = inboundOrderService.queryImportDeclareRuleList(command);

        if (list == null || list.size() == 0) {
            return SUCCESS;
        }

        List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
        for (ImportDeclarationRulesVo importDeclarationRulesVo : list) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("code", importDeclarationRulesVo.getCode());
            map.put("name", importDeclarationRulesVo.getName());
            map.put("iorCode", importDeclarationRulesVo.getIorCode());
            map.put("iorName", importDeclarationRulesVo.getIorName());
            mapList.add(map);
        }

        responseMsg.setData(mapList);
        return SUCCESS;
    }
}
